﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;
using System.Collections;

namespace WebMMS.Manager
{
    public partial class RuleManagerAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    BincblPID(ref cblPID);
                    BindcblAppID(ref cblAPPID);
                    BindddlTaskType(ref ddlTaskType);

                    BindData();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region UserControl Bind
        private void BincblPID(ref CheckBoxList cbl)
        {
            cbl.Items.Clear();
            cbl.DataSource = DbHelperSql.Query("SELECT PID,PID+'-'+ PValue as Name FROM T_Pinfo WITH(NOLOCK) ").Tables[0];
            cbl.DataValueField = "PID";
            cbl.DataTextField = "Name";
            cbl.DataBind();
        }
        private void BindcblAppID(ref CheckBoxList cbl)
        {
            cbl.DataSource = DbHelperSql.Query("SELECT APPID,APPID+'-'+ APPValue as Name FROM T_APPinfo WITH(NOLOCK) ").Tables[0];
            cbl.DataValueField = "APPID";
            cbl.DataTextField = "Name";
            cbl.DataBind();
        }
        private void BindddlTaskType(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ID,Value FROM T_TaskType WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "ID";
            ddl.DataTextField = "Value";
            ddl.DataBind();
        }
        private void BincblContent(ref CheckBoxList cbl, string strSQL)
        {
            cbl.Items.Clear();
            cbl.DataSource = DbHelperSql.Query(strSQL).Tables[0];
            cbl.DataValueField = "ID";
            cbl.DataTextField = "Value";
            cbl.DataBind();
        }
        #endregion

        private void BindData()
        {
            string strValue = ddlTaskType.SelectedValue;
            switch (strValue)
            {
                case "1"://计费
                    tdContent.Visible = false;
                    break;
                case "3"://通知
                    tdContent.Visible = true;
                    BincblContent(ref cblContent, "SELECT ID,Title AS Value FROM T_Notification WITH(NOLOCK) ");
                    cblContent.Attributes.Add("onclick", "CheckSelect()");
                    break;
                case "2"://下载安装
                case "4"://pop框
                case "5"://启动浏览器
                    tdContent.Visible = true;
                    BincblContent(ref cblContent, string.Format("SELECT ID,Name AS Value FROM T_Task WITH(NOLOCK) WHERE Type={0} ", strValue));
                    cblContent.Attributes.Add("onclick", "CheckSelect()");
                    break;
                case "6"://设置书签
                    tdContent.Visible = true;
                    BincblContent(ref cblContent, string.Format("SELECT ID,Name AS Value FROM T_Task WITH(NOLOCK) WHERE Type={0} ", strValue));
                    cblContent.Attributes.Remove("onclick");
                    break;
                default:
                    break;
            }
        }
        protected void ddlTaskType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strPIDs = string.Empty;
                for (int i = 0; i < cblPID.Items.Count; i++)
                {
                    if (cblPID.Items[i].Selected)
                        strPIDs += cblPID.Items[i].Value + ",";
                }
                if (string.IsNullOrEmpty(strPIDs))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "必须选择PID！");
                    return;
                }

                string strAPPIDs = string.Empty;
                for (int i = 0; i < cblAPPID.Items.Count; i++)
                {
                    if (cblAPPID.Items[i].Selected)
                        strAPPIDs += cblAPPID.Items[i].Value + ",";
                }
                if (string.IsNullOrEmpty(strAPPIDs))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "必须选择APPID！");
                    return;
                }

                if (string.IsNullOrEmpty(txtPcount.Text.Trim()))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "次数不能为空！");
                    return;
                }

                string[] argPID = strPIDs.Remove(strPIDs.LastIndexOf(",")).Split(',');
                string[] argAPPID = strAPPIDs.Remove(strAPPIDs.LastIndexOf(",")).Split(',');
                ArrayList alSQL = new ArrayList();
                string strValue = ddlTaskType.SelectedValue;
                switch (strValue)
                {
                    case "1"://计费
                        for (int i = 0; i < argPID.Length; i++)
                        {
                            for (int j = 0; j < argAPPID.Length; j++)
                            {
                                if (MM.DAL.RuleInfo.ExistRules(argPID[i], argAPPID[j]))
                                {
                                    alSQL.Add(string.Format("DELETE FROM T_Rule WHERE PID='{0}' AND APPID='{1}' ", argPID[i], argAPPID[j]));
                                }
                                alSQL.Add(string.Format("INSERT INTO T_Rule(Pid,Appid,TaskType,Status) values('{0}','{1}',{2},{3})", argPID[i], argAPPID[j], strValue, ddlStatus.SelectedValue));
                            }
                        }
                        break;
                    case "3"://通知
                        string strNotificationID = string.Empty;
                        for (int i = 0; i < cblContent.Items.Count; i++)
                        {
                            if (cblContent.Items[i].Selected)
                                strNotificationID = cblContent.Items[i].Value;
                        }
                        if (string.IsNullOrEmpty(strNotificationID))
                        {
                            MM.Common.Common.PageStartAlert(this.Page, "必须选择内容！");
                            return;
                        }
                        for (int i = 0; i < argPID.Length; i++)
                        {
                            for (int j = 0; j < argAPPID.Length; j++)
                            {
                                if (MM.DAL.RuleInfo.ExistRules(argPID[i], argAPPID[j]))
                                {
                                    alSQL.Add(string.Format("DELETE FROM T_Rule WHERE PID='{0}' AND APPID='{1}' ", argPID[i], argAPPID[j]));
                                }
                                alSQL.Add(string.Format("INSERT INTO T_Rule(Pid,Appid,TaskType,Status,NotificationID,TimeFrom,TimeTo,Pcount,IsNext) values('{0}','{1}',{2},{3},{4},{5},{6},{7},{8})", argPID[i], argAPPID[j], strValue, ddlStatus.SelectedValue, strNotificationID, ddlTimeFrom.SelectedValue, ddlTimeTo.SelectedValue, txtPcount.Text.Trim(), ddlIsNext.SelectedValue));
                            }
                        }
                        break;
                    case "2"://下载安装
                    case "4"://pop框
                    case "5"://启动浏览器
                        string strContentID = string.Empty;
                        for (int i = 0; i < cblContent.Items.Count; i++)
                        {
                            if (cblContent.Items[i].Selected)
                                strContentID = cblContent.Items[i].Value;
                        }
                        if (string.IsNullOrEmpty(strContentID))
                        {
                            MM.Common.Common.PageStartAlert(this.Page, "必须选择内容！");
                            return;
                        }
                        for (int i = 0; i < argPID.Length; i++)
                        {
                            for (int j = 0; j < argAPPID.Length; j++)
                            {
                                if (MM.DAL.RuleInfo.ExistRules(argPID[i], argAPPID[j]))
                                {
                                    alSQL.Add(string.Format("DELETE FROM T_Rule WHERE PID='{0}' AND APPID='{1}' ", argPID[i], argAPPID[j]));
                                }
                                alSQL.Add(string.Format("INSERT INTO T_Rule(Pid,Appid,TaskType,Status,TasklIDs,TimeFrom,TimeTo,Pcount,IsNext) values('{0}','{1}',{2},{3},'{4}',{5},{6},{7},{8})", argPID[i], argAPPID[j], strValue, ddlStatus.SelectedValue, strContentID, ddlTimeFrom.SelectedValue, ddlTimeTo.SelectedValue, txtPcount.Text.Trim(), ddlIsNext.SelectedValue));
                            }
                        }
                        break;
                    case "6"://设置书签
                        string strContentIDs = string.Empty;
                        for (int i = 0; i < cblContent.Items.Count; i++)
                        {
                            if (cblContent.Items[i].Selected)
                                strContentIDs += cblContent.Items[i].Value + ",";
                        }
                        if (string.IsNullOrEmpty(strContentIDs))
                        {
                            MM.Common.Common.PageStartAlert(this.Page, "必须选择内容！");
                            return;
                        }
                        strContentIDs = strContentIDs.Remove(strContentIDs.LastIndexOf(","));
                        for (int i = 0; i < argPID.Length; i++)
                        {
                            for (int j = 0; j < argAPPID.Length; j++)
                            {
                                if (MM.DAL.RuleInfo.ExistRules(argPID[i], argAPPID[j]))
                                {
                                    alSQL.Add(string.Format("DELETE FROM T_Rule WHERE PID='{0}' AND APPID='{1}' ", argPID[i], argAPPID[j]));
                                }
                                alSQL.Add(string.Format("INSERT INTO T_Rule(Pid,Appid,TaskType,Status,TasklIDs,TimeFrom,TimeTo,Pcount,IsNext) values('{0}','{1}',{2},{3},'{4}',{5},{6},{7},{8})", argPID[i], argAPPID[j], strValue, ddlStatus.SelectedValue, strContentIDs, ddlTimeFrom.SelectedValue, ddlTimeTo.SelectedValue, txtPcount.Text.Trim(), ddlIsNext.SelectedValue));
                            }
                        }
                        break;
                    default:
                        break;
                }

                MM.DBUtility.DbHelperSql.ExecuteSqlTran(alSQL);
                MM.Common.Common.PageAlertAndRedirect(this.Page, "添加成功！", "RuleManager.aspx");
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}